package com.alifi.crm.dal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 枚举工具
 * @author wenjing.huangwj
 *
 */
public class EnumUtils {

    private static final Log logger = LogFactory.getLog(EnumUtils.class);


    /**
     * 通过枚举code查找枚举对象
     * @param enumClass
     * @param code
     * @param <E> 所有继承枚举统一接口 EnumInterface
     * @return 查找到得枚举对象,如果查不到将返回null
     */
    public static <E extends EnumInterface> E findByCode(Class<E> enumClass, String code) {
        for (E enumeration : enumClass.getEnumConstants()) {
            if (enumeration.code().equals(code)) {
                return enumeration;
            }
        }
        StringBuffer sb = new StringBuffer();
        sb.append("non-existing code [");
        sb.append(code);
        sb.append("] in enum type [");
        sb.append(enumClass.getName());
        sb.append("]");
        logger.warn(sb.toString());
        return null;
    }

	
}
